package com.yuneec.droneservice.command;

import com.MAVLink.yuneec.msg_mav_rc_cmd_ack;
import com.yuneec.droneservice.model.ControllerVersion;
import com.yuneec.droneservice.request.IResponse;

import java.nio.charset.Charset;

/**
 * Created by gjw on 2017/3/27.
 */

public class GetControllerVersion extends BaseCommand {


    public GetControllerVersion() {
        super(MAV_RC_COMMAND.MAV_RC_CMD_GET_VERSION);
    }

    @Override
    protected short[] body() {
        return null;
    }

    @Override
    public IResponse<ControllerVersion> toResponse(msg_mav_rc_cmd_ack ack) {
        int code = ack.code;
        IResponse<ControllerVersion> response = new IResponse(ids(), code);
        if (code == MAV_RC_ERROR_CODE.MAV_RC_ERR_SUCCESS) {
            try {
                ControllerVersion version = new ControllerVersion();
                Charset utf8 = Charset.forName("utf8");
                int index = 0;
                int len = uIntArrayToUShort(ack.params, index);
                index += 2;
                byte[] buf = uIntArrayToByteArray(ack.params, index, len);
                index += len;
                version.setHardwareVersion(new String(buf, utf8));
                len = uIntArrayToUShort(ack.params, index);
                index += 2;
                buf = uIntArrayToByteArray(ack.params, index, len);
                index += len;
                version.setFirmwareVersion(new String(buf, utf8));
                len = uIntArrayToUShort(ack.params, index);
                index += 2;
                buf = uIntArrayToByteArray(ack.params, index, len);
                version.setMcuVersion(new String(buf, utf8));
                response.setParam(version);
            } catch (Exception e) {
                response.setCode(MAV_RC_ERROR_CODE.MAV_RC_ERR_UNKNOW);
            }
        }
        return response;
    }

}
